package dao

import (
	"github.com/astaxie/beego/orm"
	"xiaoercaidan/publicResources/models"
)

func InserRing(ring *models.Ring) (error) {

	o := orm.NewOrm()

	_, err := o.Insert(ring)

	return err

}

func UpdateRing(ring *models.Ring) (error) {

	o := orm.NewOrm()

	_, err := o.QueryTable("ring").Filter("id", ring.Id).Update(orm.Params{
		"shop_id": ring.ShopId,
		"remark": ring.Remark,
		"cate_id": ring.CateId,
		"ring_name": ring.RingName,
	})

	return err

}

func QeryRing(params map[string]string) (*[]models.Ring) {

	o := orm.NewOrm()

	var rings []models.Ring

	var qs orm.QuerySeter

	qs = o.QueryTable("ring")

	for k, v := range params {

		if (k == "ring_name") {
			qs = qs.Filter(k + "__icontains", v)
		} else {
			qs = qs.Filter(k + "__exact", v)
		}

	}

	qs.All(&rings)

	if (len(rings) > 0) {
		return &rings
	} else {
		return nil
	}

}

func DeleteRing(shopId, ids string) (error) {

	o := orm.NewOrm()

	sql := "delete from ring where shop_id = " + shopId + " and  id in(" + ids + ")"

	_, err := o.Raw(sql).Exec()

	return err
}

func InserRingCate(ring *models.RingCate) (error) {

	o := orm.NewOrm()

	_, err := o.Insert(ring)

	return err

}

func UpdateRingCate(ring *models.RingCate) (error) {

	o := orm.NewOrm()

	_, err := o.QueryTable("ring_cate").Filter("id", ring.Id).Update(orm.Params{
		"shop_id": ring.ShopId,
		"remark": ring.Remark,
		"cate_name": ring.CateName,
	})

	return err

}

func QeryRingCate(params map[string]string) (*[]models.RingCate) {

	o := orm.NewOrm()

	var rings []models.RingCate

	var qs orm.QuerySeter

	qs = o.QueryTable("ring_cate")

	for k, v := range params {

		if (k == "cate_name") {
			qs = qs.Filter(k + "__icontains", v)
		} else {
			qs = qs.Filter(k + "__exact", v)
		}

	}

	qs.All(&rings)

	if (len(rings) > 0) {
		return &rings
	} else {
		return nil
	}

}

func DeleteRingCate(shopId, ids string) (error) {

	o := orm.NewOrm()

	sql := "delete from ring_cate where shop_id = " + shopId + " and  id in(" + ids + ")"

	_, err := o.Raw(sql).Exec()

	return err
}

